package com.fick.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fick.hibernate.model.Admin;

public class SessionFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResonse = (HttpServletResponse) response;
		HttpSession session = httpRequest.getSession();
		// AdminDAO adminDAO = new AdminDAO();
		String urlStr = httpRequest.getRequestURI();
		Admin admin = new Admin();
		try {
			admin = (Admin) session.getAttribute("admin");
			String url = httpRequest.getRequestURL().toString();
			if (session.getAttribute("admin") == null) {
				httpResonse.sendRedirect("/login.jsp");
				return;
			} else {
				filterchain.doFilter(request, response);
				return;
			}
		} catch (Exception es) {
			es.printStackTrace();
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}
